package org.swimv2.servlets;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.entities.Job;
import org.swimv2.entities.Skill;
import org.swimv2.entities.User;
import org.swimv2.interfaces.FriendshipManagerRemote;
import org.swimv2.interfaces.JobsManagerRemote;
import org.swimv2.interfaces.SearchManagerRemote;
import org.swimv2.interfaces.SkillManagerRemote;
import org.swimv2.utils.ContextUtil;

/**
 * Servlet implementation class ShowUserServlet
 */
public class ShowUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private User curr_u, page_u;
	
	private SearchManagerRemote     mgr_u = null;
	private FriendshipManagerRemote mgr_f = null;
	private JobsManagerRemote       mgr_j = null;
	private SkillManagerRemote      mgr_s = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ShowUserServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		try {
			Context ctx = ContextUtil.getInitialContext();
			mgr_u = (SearchManagerRemote)     ctx.lookup("SearchManagerJNDI");
			mgr_f = (FriendshipManagerRemote) ctx.lookup("FriendshipManagerJNDI");
			mgr_j = (JobsManagerRemote)       ctx.lookup("JobsManagerJNDI");
			mgr_s = (SkillManagerRemote)      ctx.lookup("SkillManagerJNDI");
			
			// L'utente loggato
			curr_u = (User) request.getSession().getAttribute("user");
			// L'utente da visualizzare
			page_u = mgr_u.getUserById( Integer.parseInt(request.getParameter("user_id")) );
			
			RequestDispatcher disp;
			
			/* Reindirizza su profile.jsp se visualizziamo l'utente loggato */
			if (page_u != null && curr_u != null && page_u.getId() == curr_u.getId())
			{
				addToRequestProfile(request);
				disp = request.getRequestDispatcher("user/profile.jsp");
			}
			/* Reindirizza su show.jsp per visualizzare la pagina di un altro utente */
			else if ( page_u != null )
			{
				request.setAttribute("page_user", page_u);
				addToRequestShow(request);
				disp = request.getRequestDispatcher("user/show.jsp?u_id="+ page_u.getId());
			}
			/* Altrimenti segnala l'errore */
			else {
				request.setAttribute("error_message", "Errore: utente non trovato");
				disp = request.getRequestDispatcher("user/show.jsp?u_id="+ page_u.getId());
			}
			// Effettua il redirect
			disp.forward(request, response);
		}
		catch (NamingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doGet(request, response);
	}


	/**
	 * Aggiunge alla HTTP Request i parametri necessari a user/show.jsp
	 * per la visualizzazione di un dato utente
	 * 
	 * @param request
	 */
	protected void addToRequestShow(HttpServletRequest request) {
		request.setAttribute("active_skills", (List<Skill>) mgr_s.listAllActiveSkills());
		request.setAttribute("page_user_skills", (List<Skill>) mgr_s.listUserSkills(page_u));
		request.setAttribute("page_user_jobs", (List<Job>) mgr_j.listJobsWork(page_u));
		request.setAttribute("is_friend", mgr_f.isFriendOf(curr_u, page_u));
	}
	
	/**
	 * Aggiunge alla HTTP Request i parametri necessari a user/profile.jsp
	 * per la visualizzazione del profilo dell'utente loggato.
	 * 
	 * @param request
	 */
	protected void addToRequestProfile(HttpServletRequest request) {
		request.setAttribute("active_skills", (List<Skill>) mgr_s.listAllActiveSkills());
		request.setAttribute("curr_user_skills", (List<Skill>) mgr_s.listUserSkills(curr_u));
		request.setAttribute("curr_user_jobs", (List<Job>) mgr_j.listJobsWork(curr_u));
		request.setAttribute("curr_user_friendreqs", (List<User>) mgr_f.listFriendshipReq(curr_u));
	}
}
